package org.hgm.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.social.connect.Connection;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.Post;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
import org.springframework.social.oauth2.AccessGrant;
import org.springframework.social.oauth2.GrantType;
import org.springframework.social.oauth2.OAuth2Operations;
import org.springframework.social.oauth2.OAuth2Parameters;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class FaceBookController {

	/*@RequestMapping(value = "/fb.htm", method = RequestMethod.GET)
	public @ResponseBody
	Facebook getFacebook() {
		String accessToken = "CAACEdEose0cBAPDSPwNirUH2seLigoXChYrBCrcgQbbVrj0ZBJpTYZBlCthazg0fCPZB2plOULO3VsE2z4cCZBfQFpzFOTuD98VMRL05LRvgLuqUTDPxgShsYhijfkEdxbXuBaJIs7ZAIJVAi3MOKhBVq8C6vJfIZD"; // access
																																																	// authorization
		Facebook facebook = new FacebookTemplate(accessToken);
		List<String> friendIds = facebook.friendOperations().getFriendIds();
		String myId = facebook.userOperations().getUserProfile().getId();
		List<Post> feed = facebook.feedOperations().getFeed(myId);
		for (Post p : feed) {
			System.out.println("message:" + p.getMessage());
			System.out.println("description :" + p.getDescription());
			System.out.println("like count:" + p.getLikeCount());
		}

		for (String friendId : friendIds) {
			FacebookProfile firstFriend = facebook.userOperations().getUserProfile(friendId);
			System.out.println(firstFriend.getFirstName());
			try {
				List<Post> feed2 = facebook.feedOperations().getFeed(friendId);
				for (Post p : feed2) {
					System.out.println("message:" + p.getMessage());
					System.out.println("description :" + p.getDescription());
					System.out.println("like count:" + p.getLikeCount());
				}
			} catch (Exception e) {
				System.out.println("error");
			}
			System.out.println("===============================================");
		}
		return facebook;
	}*/
	FacebookConnectionFactory connectionFactory = 
		    new FacebookConnectionFactory("101131350095492", "03f5447ab4cf60551fb1b8d236435a72");
	
	@RequestMapping(value = "/fb.htm", method = RequestMethod.GET)
	public void test(HttpServletResponse response) throws IOException{
			OAuth2Operations oauthOperations = connectionFactory.getOAuthOperations();
			OAuth2Parameters params = new OAuth2Parameters();
		params.setRedirectUri("http://localhost:8080/hgm/fb2.htm");
			String authorizeUrl = oauthOperations.buildAuthorizeUrl(GrantType.AUTHORIZATION_CODE, params);
			response.sendRedirect(authorizeUrl);
	}
	
	@RequestMapping(value = "/fb2.htm", method = RequestMethod.GET)
	public void test2(String code) throws IOException {
		OAuth2Operations oauthOperations = connectionFactory.getOAuthOperations();		
		AccessGrant accessGrant = oauthOperations.exchangeForAccess(code, "http://localhost:8080/hgm/fb2.htm", null);
		Connection<Facebook> connection = connectionFactory.createConnection(accessGrant);
		
		connection.getApi().userOperations().getUserProfile();
		List<String> friendIds = connection.getApi().friendOperations().getFriendIds();
		for (String friendId : friendIds) {
			System.out.println(connection.getApi().userOperations().getUserProfile(friendId).getFirstName());
			List<Post> feeds = connection.getApi().feedOperations().getFeed(friendId);
			for (Post p : feeds) {
				System.out.println("MSG:::::::::" + p.getMessage());
				System.out.println("LKC:::::::::" + p.getLikeCount());
				System.out.println("NAM:::::::::" + p.getName());
				// System.out.println("COM:::::::::" + p.getComments() != null ? p.getComments().size() : 0);
				System.out.println("LIN:::::::::" + p.getLink());
			}
		}

	}
	
	/*
	 * @RequestMapping(value = "/tw.htm", method = RequestMethod.GET) public void test3(HttpServletResponse response) throws IOException {
	 * 
	 * OAuth1Operations oauthOperations = connectionFactoryTwitter.getOAuthOperations(); String authorizeUrl =
	 * oauthOperations.buildAuthorizeUrl("http://127.0.0.1:8080/hgm/tw1.htm", OAuth1Parameters.NONE); response.sendRedirect(authorizeUrl);
	 * 
	 * Twitter twitter = new TwitterTemplate("QkjRB3ptAfSNByPcuQhZHw", "jNe1If9nVDQQoRGUz7eJNXY8U1HPaw4Cr1CzjdiHxs",
	 * "142276948-lOGYE7ixRCqkqvBPANsxTwNO5jCYKqomjlkdqfLV", "1rKb0VVYg8GCWiflP2HEkQDBkOFqHR6we3KUOkiaRI"); TwitterProfile profile =
	 * twitter.userOperations().getUserProfile(); List<Tweet> tweets = twitter.timelineOperations().getHomeTimeline(); tweets =
	 * twitter.timelineOperations().getUserTimeline("mansunil"); }
	 */

	/*
	 * @RequestMapping(value = "/tw1.htm", method = RequestMethod.GET) public void test4(@RequestParam String code) throws IOException {
	 * OAuth1Operations oauthOperations = connectionFactoryTwitter.getOAuthOperations(); OAuthToken requestToken =
	 * oauthOperations.fetchRequestToken("http://127.0.0.1:8080/hgm/tw1.htm", null); OAuthToken accessToken =
	 * oauthOperations.exchangeForAccessToken(new AuthorizedRequestToken(requestToken, "http://localhost:8080/hgm/tw1.htm"), null);
	 * Connection<Twitter> connection = connectionFactoryTwitter.createConnection(accessToken);
	 * 
	 * 
	 * }
	 */

}
